<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:include="common/common_head :: commonHeader('货源记录录入')"/>
    <link th:href="@{/static/css/plugins/cropper/cropper.min.css}" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <form class="form-horizontal" method="post" id="SupplyGoodForm">
        <div class="row">
            <div class="form-group">
                <div class="col-sm-8" style="margin-left: 16px">
                    <input class="btn btn-primary" type="button" onclick="save()" value="保存">
                    <button class="btn btn-primary" type="button" onclick="Dialog.closeFrame(window.name)">取 消</button>
                </div>
            </div>
            <!-- 显示 隐藏测试 -->
            <ul id="myTab" class="nav nav-tabs">
                <li class="active">
                    <a href="#basicinformation" linkTab="basicinformation" isActive="true" data-toggle="tab"> 基础信息</a>
                </li>
            </ul>
            <!-- 基本信息 -->
            <div id="basicinformation">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>物料号：</label>
                            <div class="col-xs-3">
                                <input id="materialCode" name="materialCode" type="text" class="form-control required">
                                <input type="hidden" name="createdBy" th:value="${username}">
                            </div>
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>物料描述：</label>
                            <div class="col-xs-3">
                                <input id="materialDesc" type="text" class="form-control" disabled>
                            </div>
                            <label class="col-sm-1 control-label"><span class="required-sign">*</span>工厂：</label>
                            <div class="col-sm-3">
                                <input type="hidden" id="werks" name="werks">
                                <select id="gc" class="chosen-select" data-placeholder="选择工厂" hassubinfo="true" multiple
                                        style="width: 100%" tabindex="4" required>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>供应商编码：</label>
                            <div class="col-xs-3">
                                <input id="srmCode" name="srmCode" type="text" class="form-control required">
                            </div>
                            <label class="col-xs-1 control-label"><span class="required-sign">*</span>供应商名称：</label>
                            <div class="col-xs-3">
                                <input id="vendorName" type="text" class="form-control" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label"><span class="required-sign">*</span>有效日期开始：</label>
                            <div class="col-sm-3">
                                <input class="form-control required" id="startData" name="startData" autocomplete="off">
                            </div>
                            <label class="col-sm-1 control-label"><span class="required-sign">*</span>有效日期结束：</label>
                            <div class="col-sm-3">
                                <input class="form-control required" id="priceEndData" name="priceEndData"
                                       value="9999-12-31" disabled
                                       autocomplete="off"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<!-- Chosen -->
<script th:src="@{/static/js/plugins/chosen/chosen.jquery.js}"></script>
<script type="text/javascript">

    $(function () {
        // 初始化时间插件
        laydate.render({elem: '#startData'});
        // 初始化时间插件
        laydate.render({elem: '#priceEndData'});
        //初始化所有字典数据
        initDictDataSel();

    });

    //根据vendorCode查vendorName
    $("#srmCode").change(function () {
        let vendorCode = $(this).val();
        $.ajax({
            url: ctxPath + "/vendor/findById/" + vendorCode,
            type: 'POST',
            dataType: 'JSON',
            success: function (data) {
                if (data.result) {
                    $("#vendorName").val(data.result.vendorName);
                } else {
                    Dialog.errorMsg("不存在该供应商，请确认!");
                }
            }
        });
    });

    //获取工厂列表
    Page.ajaxPOST(ctxPath + "/sys/dict/getDictData", {
        'dictTypeName': "GC"
    }, function (res) {
        var html = '';
        for (var k in res.result) {
            html += '<option value="' + k + '" hassubinfo="true">' + res.result[k] + '</option>'
        }
        $("#gc").append(html);
        $("#gc").chosen();
    });

    //保存
    function save() {
        if ($("#SupplyGoodForm").valid()) {
            var werksList = $("#gc").val();
            console.log($('#SupplyGoodForm').serialize())
            if (werksList == null) {
                Dialog.errorMsg("请选择工厂");
                return;
            }
            $("#werks").val(werksList);
            Page.ajaxPOST(ctxPath + "/supplyGood/save", $('#SupplyGoodForm').serialize(), function (res) {
                if (res.code == 0) {
                    Dialog.msg("保存成功!");
                    parent.refresh();
                    Dialog.closeFrame(window.name);
                } else {
                    Dialog.errorMsg(res.result);
                }
            }, {"loadingMsg": "正在往服务器提交数据……"});
        }
    }

    //根据vendorCode查vendorName
    $("#srmCode").change(function () {
        let vendorCode = $(this).val();
        $.ajax({
            url: ctxPath + "/vendor/findById/" + vendorCode,
            type: 'POST',
            dataType: 'JSON',
            success: function (data) {
                if (data.result) {
                    $("#vendorName").val(data.result.vendorName);
                } else {
                    Dialog.errorMsg("不存在该供应商，请确认!");
                }
            }
        });
    });

    //根据materialCode查materialDesc
    $("#materialCode").change(function () {
        let materialCode = $(this).val();
        $.ajax({
            url: ctxPath + "/material/getMaterialByCode",
            data: {"materialCode": materialCode},
            dataType: 'JSON',
            success: function (data) {
                if (data.result) {
                    $("#materialDesc").val(data.result.materialDesc);
                } else {
                    Dialog.errorMsg("不存在该物料，请确认!");
                }
            }
        });
    });


</script>

</body>
</html>
